<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png" alt=""/>
            </div>
            <el-form ref="loginFormRef" :rules="loginRules" :model="loginForm" class="login_form" label-width="0">
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" prefix-icon="el-icon-search"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="loginForm.password" prefix-icon="el-icon-search" type="password"></el-input>
                    </el-form-item>
                    <el-form-item class="btns">
                        <el-button type="primary" @click="login">提交</el-button>
                        <el-button type="info" @click="resetLoginForm">重置</el-button>
                    </el-form-item>
                </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            loginForm: {
                username: "admin",
                password: "123456"
            },
            loginRules: {
                username: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' },
                    { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入用户密码', trigger: 'blur' },
                    { min: 6, max: 10, message: '长度在 6 ~ 10 个字符', trigger: 'blur' }
                ],
            },
        }
    },
    methods: {
        resetLoginForm() {
            this.$refs.loginFormRef.resetFields()
        },
        login() {
            this.$refs.loginFormRef.validate(async valid => {
                if (!valid) return
                const { data: res } = await this.$http.post('login', this.loginForm);
                if (res.flag == "ok") {
                const user = res.user; // ✅ 正确定义 user 变量
                this.$message.success('登录成功')

                // 存储用户信息到 sessionStorage（可选）
                window.sessionStorage.setItem("user", JSON.stringify(user))

                // 存储用户名作为 employeeId
                localStorage.setItem('employeeId', user.username)

                console.log(user.username) // ✅ 使用 console.log 并输出 username

                this.$router.push('/home')
            } else {
                    this.$message.error('登录失败')
                }

            })
        }
    }
}
    
</script>
export default {

}

<style lang = "less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}
.login_box {
    width: 450px;
    padding: 20px;
    background-color: #fff;
    border-radius: 3px;
    box-shadow: 0 0 10px rgba(0,0,0,.1);
    display: flex;
    flex-direction: column;
    align-items: center;
}

.avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 2px #ddd;
    background-color: #0ee;
    margin-bottom: 20px;
    img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        object-fit: cover;
        background-color: #eee;
    }
}

.btns{
    display: flex;
    justify-content: flex-end;
    width: 100%;
}
.loginFormRef{
    position : absolute;
    bottom: 0%;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
}

</style>